package com.example.zqwl;

import com.example.handler.InServerHandler;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.handler.InServerHandler;

public class ConfigCommand {

	//1.获取设备基本信息info
	public static String command_GetBasicInfo(String address) {
		String command="{\"rmtcmd\":\"get\",\"dev\":[\"info\"]";
		String imei=null;
		if(InServerHandler.ip_conn_Map.get(address)!=null)
			imei=InServerHandler.ip_conn_Map.get(address).getImei();
		if(imei!=null)
			command+=",\"imei\":\""+imei+"\"}";
//		System.out.println(command);
		else
			command+="}";
		return command;
	}

	//2.获取sa、mqtt服务器连接以及串口信息
	public static String command_GetServerConfig(String address) {
		String command="{\"rmtcmd\":\"get\",\"dev\":[\"sa\",\"mqtt\",\"uart\"]";
		String imei=null;
		if(InServerHandler.ip_conn_Map.get(address)!=null)
			imei=InServerHandler.ip_conn_Map.get(address).getImei();
		if(imei!=null) //
			command+=",\"imei\":\""+imei+"\"}";
		else
			command+="}";
		return command;
	}

	//3.设置sa、mqtt服务器连接以及串口参数
	public static String command_ServerConfig(String address, dev_server_info deploy) {
		//dev_server_info(mode=0, sa_ip=192.168, sa_port=1000, udp=0, rate=9600, check=1, dataw=8, stopw=1, pack=0, m_ip=192.168.0.1, m_port=1883, un=wl, up=wl, id=1, ka=5, sub=ok, pub1=ok)
		String command="{\"rmtcmd\":\"set\",\"dev\":{";
		//JSONObject jsonObject=(JSONObject) obj;
		String str=null;
		int value=0;

		value=deploy.getMode();
		command+="\"wm\":{\"mode\":"+value+"}";
		//sa服务器
		str=deploy.getSa_ip();
		if(str!=null) {
			command+=",\"sa\":{\"enable\":1,\"ip\":\""+str+"\"";
			value=deploy.getSa_port();
			command+=",\"port\":"+value;

			command+=",\"udp\":0}";
		}
		//串口参数
		value=deploy.getRate();
		command+=",\"uart\":{\"rate\":"+value;
		value=deploy.getCheck();
		command+=",\"check\":"+value;
		value=deploy.getPack();
		command+=",\"pack\":"+value;
		command+="}";

		//mqtt服务器
		str=deploy.getM_ip();
		if(str!=null) {
			command+=",\"mqtt\":{\"enable\":1,\"ip\":\""+str+"\"";
			value=deploy.getM_port();
			command+=",\"port\":"+value;

			str=deploy.getUn();
			if(str!=null) {
				command+=",\"un\":\""+str+"\"";
			}
			str=deploy.getUp();
			if(str!=null) {
				command+=",\"up\":\""+str+"\"";
			}
			str=deploy.getId();
			if(str!=null) {
				command+=",\"id\":\""+str+"\"";
			}
			value=deploy.getKa();
			command+=",\"ka\":"+value;

			str=deploy.getSub();
			if(str!=null) {
				command+=",\"sub\":\""+str+"\"";
			}
			str=deploy.getPub1();
			if(str!=null) {
				command+=",\"pub1\":\""+str+"\"";
			}
			command+="}";
		}
		command+=",\"flash\":1}";
		String imei=null;
	if(InServerHandler.ip_conn_Map.get(address)!=null)
			imei=InServerHandler.ip_conn_Map.get(address).getImei();
		if(imei!=null)
			command+=",\"imei\":\""+imei+"\"}";
		else
			command+="}";

		return command;
	}

	//4.获取modbus轮询配置基本信息
	public static String command_GetModbusBasicInfo(String address) {
			String command="{\"rmtcmd\":\"get\",\"dev\":[\"poll\"]";
			String imei=null;
			if(InServerHandler.ip_conn_Map.get(address)!=null)
				imei=InServerHandler.ip_conn_Map.get(address).getImei();
			if(imei!=null)
				command+=",\"imei\":\""+imei+"\"}";
			else
				command+="}";
			return command;
		}

	//5.设置modbus轮询配置基础命令
	public static dev_modbus_basic_info PrepareModbusConfig(String content) {
		//示例:{"timing":"5","change":"5","uptype":"5","overtime":"5",
			//"poll":"5","prefix":"f","orderDelay":"0","shadowingAddr":"0",
			//"data":[{"location":"","func":"","sr":"0x2000","srnum":"2","datatype":"int","jsonname":"test","oob":"","k":"","b":"","cache":"","LAY_TABLE_INDEX":0},
			//{"location":"","func":"","sr":"0x2004","srnum":"2","datatype":"int","jsonname":"test2","oob":"","k":"","b":"","cache":"","LAY_TABLE_INDEX":1}]}
		dev_modbus_basic_info info=new dev_modbus_basic_info();
		JSONObject jsonObject = JSONObject.parseObject(content);
		String str=null;
		str=jsonObject.getString("uploadtime");
		if(str!=null&&!str.isEmpty()){
			info.setUploadtime(Integer.parseInt(str));
		}
		str=jsonObject.getString("change");
		if(str!=null&&!str.isEmpty()){
			if(str.equalsIgnoreCase("不启用"))
				info.setUploadchange(0);  //变化上传
			else
				info.setUploadchange(1);  //变化上传
		}
		str=jsonObject.getString("uptype");
		if(str!=null&&!str.isEmpty()){
			if(str.equalsIgnoreCase("modbus rtu 地址64 功能码03"))
				info.setUploadtype(0);  //上传类型
			else if(str.equalsIgnoreCase("json 带IMEI"))
				info.setUploadtype(1);  //上传类型
			else if(str.equalsIgnoreCase("阿里云 json Alink"))
				info.setUploadtype(2);  //上传类型
			else if(str.equalsIgnoreCase("Vlink"))
				info.setUploadtype(3);  //上传类型
			else if(str.equalsIgnoreCase("塔吊json+前缀"))
				info.setUploadtype(4);  //上传类型
			else if(str.equalsIgnoreCase("json+前缀 自定义格式(%s)"))
				info.setUploadtype(5);  //上传类型
			else if(str.equalsIgnoreCase("HJ212"))
				info.setUploadtype(6);  //上传类型
			else
				;
		}
		str=jsonObject.getString("poll");
		if(str!=null&&!str.isEmpty()){
			info.setUpdate(Integer.parseInt(str));  //轮询间隔
		}
		str=jsonObject.getString("overtime");
		if(str!=null&&!str.isEmpty()){
			info.setTimeout(Integer.parseInt(str));  //超时
		}
		str=jsonObject.getString("orderDelay");
		if(str!=null&&!str.isEmpty()){
			info.setDelay(Integer.parseInt(str));  //指令延时
		}
		str=jsonObject.getString("timestamp");
		if(str!=null)
		{
			if(!str.isEmpty()){
			info.setTimestamp(Integer.parseInt(str));  //时间戳
			}
		}
		str=jsonObject.getString("prefix");
		if(str!=null)
			info.setPrefix(str);  //前缀
		else
			info.setPrefix("\"{\"method\\\":\"report\",\"params\":{\"id\":%i,%d}}\"");
		str=jsonObject.getString("cmdcnt");
		if(str!=null&&!str.isEmpty())
		{
			info.setCmdcnt(Integer.parseInt(str));  //设置的轮询寄存器(cmd)总条数
		}
		return info;
	}
	public static String command_ModbusBasicConfig(String address,dev_modbus_basic_info info) {

		//设置modbus轮询基础信息
		String command="{\"rmtcmd\":\"set\",\"dev\":{\"poll\":{";

		command+="\"uploadtime\":"+info.getUploadtime();  //定时上传时间
		command+=",\"uploadchange\":"+info.getUploadchange();  //变化上传
		command+=",\"uploadtype\":"+info.getUploadtype();  //上传类型
		command+=",\"update\":"+info.getUpdate();  //轮询间隔
		command+=",\"timeout\":"+info.getTimeout();  //超时
		command+=",\"delay\":"+info.getDelay();  //指令延时
		command+=",\"timestamp\":"+info.getTimestamp();  //时间戳
		command+=",\"prefix\":"+"\"{\\\"method\\\":\\\"report\\\",\\\"params\\\":{\\\"id\\\":%i,%d}}\"";  //前缀
		command+="},\"flash\":1}";

		String imei=null;
//		if(InServerHandler.ip_conn_Map.get(address)!=null)
//			imei=InServerHandler.ip_conn_Map.get(address).getImei();
		if(imei!=null)
			command+=",\"imei\":\""+imei+"\"}";
		else
			command+="}";

		return command;
	}

	//6.获取modbus轮询配置寄存器命令
	public static String[] command_GetModbusCmd(String address,int cmdcnt) {
		//先执行command_GetModbusBasicInfo获取到命令总数cmdcnt，再调用本方法
		String[] command_array=new String[cmdcnt];
		String command0="{\"rmtcmd\":\"get\",\"dev\":[\"pollcmd\"]";
		for(int i=0;i<cmdcnt;i++) {
			String command=command0;
			command+=",\"cmdidx\":"+i;
			String imei=null;
//			if(InServerHandler.ip_conn_Map.get(address)!=null)
//				imei=InServerHandler.ip_conn_Map.get(address).getImei();
			if(imei!=null)
				command+=",\"imei\":\""+imei+"\"}";
			else
				command+="}";

			command_array[i]=command;
		}
		return command_array;

	}

	//7.设置modbus轮询配置寄存器扫描添加/删除/更新命令
	//cnt=cnt+1,idx=idx+1则添加；cnt=cnt-1,idx=idx-1则删除最后一条
	//添加和删除就在cmdlist上操作，最后再更新到设备。
	public static void command_ModbusCmdAdd(dev_modbus_cmd_list list,dev_modbus_cmd_info info) {
		//传入的是待添加的命令信息
		list.modbus_cmd_add(info);
	}
	public static void command_ModbusCmdDel(dev_modbus_cmd_list list,int index) {
		//传入的是待添加的命令信息
		list.modbus_cmd_delete(index);
	}
	public static String[] command_ModbusUpdate(String address,dev_modbus_cmd_list list) {
		//设置modbus扫描命令
		int cmdtotalnumbers=list.getCmdcnt();
		String[] command_array=new String[cmdtotalnumbers];
		String command0="{\"rmtcmd\":\"set\",\"dev\":{\"pollcmd\":{\"cmd\":{";

		for(int i=0;i<cmdtotalnumbers;i++) {
			String command=command0;
			command+="\"cnt\":" + (i+1);
			command+=",\"idx\":" + i;
			command+=",\"addr\":" + list.getCmdlist().get(i).getAddr();
			command+=",\"fun\":" + list.getCmdlist().get(i).getFun();
			command+=",\"raddr\":" + list.getCmdlist().get(i).getRaddr();
			command+=",\"rsize\":" + list.getCmdlist().get(i).getRsize();
			command+=",\"dtype\":" + list.getCmdlist().get(i).getDtype();
			command+=",\"float\":" + list.getCmdlist().get(i).get_float();
			command+=",\"key\":\"" + list.getCmdlist().get(i).getKey()+"\"";
			command+=",\"tv1\":" + list.getCmdlist().get(i).getTv1();
			command+=",\"tv2\":" + list.getCmdlist().get(i).getTv2();
			command+=",\"em\":" + list.getCmdlist().get(i).getEm();
			command+="}},\"flash\":1}";
			String imei=null;
			if(InServerHandler.ip_conn_Map.get(address)!=null)
				imei=InServerHandler.ip_conn_Map.get(address).getImei();
			if(imei!=null)
				command+=",\"imei\":\""+imei+"\"}";
			else
				command+="}";

			command_array[i]=command;
		}

		return command_array;
	}
    public static dev_modbus_cmd_list command_ModbusUpdate_prepare(String items) {
    	//典型页面获得的字符串
    	//{"uploadtime":"5","change":"1","uptype":"6","overtime":"5","poll":"5","prefix":"5","orderDelay":"0","shadowingAddr":"0",
    	//"data":[{"location":"","func":"","sr":"0x2000","srnum":"2","datatype":"int","jsonname":"test","oob":"","k":"","b":"","cache":"","LAY_TABLE_INDEX":0},
    	//{"location":"","func":"","sr":"0x2004","srnum":"2","datatype":"int","jsonname":"test2","oob":"","k":"","b":"","cache":"","LAY_TABLE_INDEX":1}]}
    	dev_modbus_cmd_list l=new dev_modbus_cmd_list();
    	JSONObject jsonObject = JSONObject.parseObject(items);
    	JSONArray data=jsonObject.getJSONArray("data");
    	System.out.println(data.size());
    	for(int i=0;i<data.size();i++) {
    		dev_modbus_cmd_info info=new dev_modbus_cmd_info();
    		info.setIdx(i);
    		JSONObject obj=(JSONObject)data.get(i);
    		String key=null;
    		key=obj.getString("location");
    		if(key!=null&&!key.isEmpty())
    			info.setAddr(Integer.parseInt(key));
    		else
    			info.setAddr(i);
    		key=obj.getString("func");
    		if(key!=null&&!key.isEmpty())
    			info.setFun(Integer.parseInt(key));
    		else
    			info.setFun(3);
    		key=obj.getString("sr");
    		if(key!=null&&!key.isEmpty())
    			info.setRaddr(Integer.parseInt(key));
    		else
    			info.setRaddr(0);
    		key=obj.getString("srnum");
    		if(key!=null&&!key.isEmpty())
    			info.setRsize(Integer.parseInt(key));
    		else
    			info.setRsize(0);
    		key=obj.getString("datatype");
    		if(key!=null&&!key.isEmpty())
    			if(key.equalsIgnoreCase("int"))
    			info.setDtype(1);
    		else
    			info.setDtype(1);
    		key=obj.getString("jsonname");
    		if(key!=null&&!key.isEmpty())
    			info.setKey(key);
    		else
    			info.setKey(null);
    		key=obj.getString("oob");
    		if(key!=null&&!key.isEmpty())
    			info.setEm(Integer.parseInt(key));
    		else
    			info.setEm(0);
    		key=obj.getString("k");
    		if(key!=null&&!key.isEmpty())
    			info.setTv1(Integer.parseInt(key));
    		else
    			info.setTv1(0);
    		key=obj.getString("b");
    		if(key!=null&&!key.isEmpty())
    			info.setTv2(Integer.parseInt(key));
    		else
    			info.setTv2(0);

    		l.modbus_cmd_add(info);
    	}
    	return l;
    }

    //8.设置重启命令
	public static String command_reset(String address) {
		String command="{\"rmtcmd\":\"reset\"";
		String imei=null;
		if(InServerHandler.ip_conn_Map.get(address)!=null)
			imei=InServerHandler.ip_conn_Map.get(address).getImei();
		if(imei!=null)
			command+=",\"imei\":\""+imei+"\"}";
		else
			command+="}";
		return command;
	}
}
